package com.skcc.ts.nsi.expense;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.skcc.ts.nsi.R;
import com.skcc.ts.nsi.service.MEDataHandler;
import com.skcc.ts.nsi.service.SocketClientService;

/**
 * <b>클래스 명 :</b> <b>MEExpenseDetail</b>
 * <p>
 * <b>용도:</b>
 * <ul>
 * <li>예산 상세정보 화면
 * </ul>
 * </p>
 * 
 * @author <a href="mailto:ksy731@sk.com">김신영</a>
 * @since 2012. 9. 12.
 */
public class MEExpenseDetail extends Activity {
	private ArrayList<String[]> pjtList = null;
	private String[] pjtCode;
	private String[] pjtName;
	private Button btn_expense_detail_search;
	private int expSearchid = 49; // 기능 분류 (예산 상세정보 조회 결과)
	private String myId;
	private String selectedPjtName;
	private String selectedPjtCode;
	// private String pjtCode;
	private TextView text_name;
	private TextView text_total;
	private TextView text_used;
	private TextView text_balance;
	private String addr;
	private String port;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.meexpense_detail);

		doListExpense(); // 프로젝트정보수집
		doDetailList(); // 예산상세정보리스팅
		initButtons(); // 버튼 세팅
		initSpinners(); // 스니퍼 동적생성
	}

	private void doDetailList() {
		Intent receiveI = getIntent();
		boolean doYN = receiveI.getBooleanExtra("detailListing", false);
		if (doYN) {// 스피너 세팅, 테이블 동적생성
			String data = receiveI.getStringExtra("dataYN");
			myId = receiveI.getStringExtra("myId");
			addr = receiveI.getStringExtra("loginAddr");
			port = receiveI.getStringExtra("loginPort");
			String[] code = receiveI.getStringArrayExtra("projectCode");
			String[] name = receiveI.getStringArrayExtra("projectName");
			ArrayList<String> transCode = new ArrayList<String>();
			ArrayList<String> transName = new ArrayList<String>();
			transCode.add("-- 선택 --");
			transName.add("-- 선택 --");
			for (int i = 0; i < code.length; i++) {
				transCode.add(code[i]);
				transName.add(name[i]);
			}
			pjtCode = (String[]) transCode.toArray(new String[(code.length) + 1]);
			pjtName = (String[]) transName.toArray(new String[(code.length) + 1]);

			if ("yes".equals(data)) {
				// pjtList = new ArrayList<String[]>();
				// String[] defVal = { "전체", "전체" };
				// pjtList.add(defVal); // 스피너 디폴트값 설정

				String[] acnt_name = receiveI.getStringArrayExtra("acntName");
				String[] acnt_total = receiveI.getStringArrayExtra("acntTotal");
				String[] acnt_balance = receiveI.getStringArrayExtra("acntBalance");

				// 테이블 생성
				TableLayout mtable = (TableLayout) findViewById(R.id.tableLayout_exp_detail_list);
				for (int i = 0; i < acnt_name.length; i++) {
					TableRow tableR = new TableRow(this);
					tableR.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
					int used = (Integer.parseInt(acnt_total[i]) - Integer.parseInt(acnt_balance[i]));
					String cache = Integer.toString(used);
					text_name = new TextView(this);
					text_total = new TextView(this);
					text_used = new TextView(this);
					text_balance = new TextView(this);
					if (i % 2 == 1) {
						text_name.setBackgroundColor(0xffd3daeb);
						text_total.setBackgroundColor(0xffd3daeb);
						text_used.setBackgroundColor(0xffd3daeb);
						text_balance.setBackgroundColor(0xffd3daeb);
					} else {
						text_name.setBackgroundColor(Color.WHITE);
						text_total.setBackgroundColor(Color.WHITE);
						text_used.setBackgroundColor(Color.WHITE);
						text_balance.setBackgroundColor(Color.WHITE);
					}
					text_name.setText(acnt_name[i]);
					text_name.setTextColor(Color.BLACK);
					text_name.setTextSize(12);
					text_name.setEms(6);
					text_total.setText(MEDataHandler.addComma(acnt_total[i]) + "원 ");
					text_total.setTextColor(Color.BLACK);
					text_total.setTextSize(12);
					text_total.setEms(6);
					text_total.setGravity(Gravity.RIGHT);

					text_used.setText(MEDataHandler.addComma(cache) + "원 ");
					text_used.setTextColor(Color.BLACK);
					text_used.setTextSize(12);
					text_used.setEms(5);
					text_used.setGravity(Gravity.RIGHT);

					text_balance.setText(MEDataHandler.addComma(acnt_balance[i]) + "원 ");
					text_balance.setTextColor(Color.BLACK);
					text_balance.setTextSize(12);
					text_balance.setEms(6);
					text_balance.setGravity(Gravity.RIGHT);

					tableR.addView(text_name);
					tableR.addView(text_total);
					tableR.addView(text_used);
					tableR.addView(text_balance);
					mtable.addView(tableR, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
				}
			} else if ("no".equals(data)) {
				new AlertDialog.Builder(MEExpenseDetail.this).setTitle("알림").setMessage("프로젝트 내 등록된 계정이 없습니다.")
						.setPositiveButton("OK", null).show();
			}
		}
	}

	private void doListExpense() {
		Intent receiveI = getIntent();
		boolean doYN = receiveI.getBooleanExtra("start", false);
		if (doYN) {
			// pjtList = new ArrayList<String[]>();
			// String[] defVal = { "전체", "전체" };
			// pjtList.add(defVal); // 스피너 디폴트값 설정
			boolean reset = receiveI.getBooleanExtra("setAcnt", false);
			myId = receiveI.getStringExtra("myId");
			addr = receiveI.getStringExtra("loginAddr");
			port = receiveI.getStringExtra("loginPort");
			if (reset) {
				new AlertDialog.Builder(MEExpenseDetail.this).setTitle("알림").setMessage("프로젝트 내 해당계정이 없습니다.").setPositiveButton("OK", null)
						.show();
			}
			String[] code = receiveI.getStringArrayExtra("projectCode");
			String[] name = receiveI.getStringArrayExtra("projectName");
			ArrayList<String> transCode = new ArrayList<String>();
			ArrayList<String> transName = new ArrayList<String>();
			transCode.add("-- 선택 --");
			transName.add("-- 선택 --");
			for (int i = 0; i < code.length; i++) {
				transCode.add(code[i]);
				transName.add(name[i]);
			}
			pjtCode = (String[]) transCode.toArray(new String[(code.length) + 1]);
			pjtName = (String[]) transName.toArray(new String[(code.length) + 1]);
		}
	}

	private void initSpinners() {// 스피너 세팅
		// final String[] pjtSnipper = pjtList.get(1);
		ArrayAdapter<String> adapter_pjt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, pjtName);
		Spinner sp_pjt = (Spinner) findViewById(R.id.spinner_expense_detail_pjtlist);
		adapter_pjt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_pjt.setAdapter(adapter_pjt);

		sp_pjt.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				selectedPjtName = pjtName[position];
				selectedPjtCode = pjtCode[position];
			}

			public void onNothingSelected(AdapterView<?> parent) {
				selectedPjtName = "-- 선택 --";
			}
		});
	}

	/**
	 * <b>메소드 :</b> <b>메소드 한글/영문 명을 여기에 기입합니다.</b>
	 * <p>
	 * <ul>
	 * <li>버튼 이벤트 정리
	 * </ul>
	 * </p>
	 * 
	 * @since 2012. 8. 14.
	 */
	private void initButtons() {
		btn_expense_detail_search = (Button) findViewById(R.id.btn_expense_detail_search);

		// 예산 상세정보 조회
		btn_expense_detail_search.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {

				if ("-- 선택 --".equals(selectedPjtCode)) {
					new AlertDialog.Builder(MEExpenseDetail.this).setTitle("알림").setMessage("조회할 프로젝트를 선택하여 주십시오.")
							.setPositiveButton("OK", null).show();
				} else {
					Intent expI = new Intent(MEExpenseDetail.this, SocketClientService.class);
					expI.putExtra("id", expSearchid);// 49
					expI.putExtra("myId", myId);
					expI.putExtra("loginAddr", addr);
					expI.putExtra("loginPort", port);
					expI.putExtra("projectCode", selectedPjtCode);
					expI.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
					startService(expI);
					finish();
				}
			}
		});
	}
}